package ird.demo.service.impl;

import ird.demo.bo.AjaxResult;
import ird.demo.common.DemoConstant;
import ird.demo.common.SysUtil;
import ird.demo.domain.Menu;
import ird.demo.domain.TranGrp;
import ird.demo.domain.TranGrpTran;
import ird.demo.domain.TranGrpTranPK;
import ird.demo.domain.Transaction;
import ird.demo.domain.User;
import ird.demo.domain.UsrGrp;
import ird.demo.domain.UsrGrpMenu;
import ird.demo.domain.UsrGrpMenuPK;
import ird.demo.domain.UsrGrpTran;
import ird.demo.domain.UsrGrpTranGrp;
import ird.demo.domain.UsrGrpTranGrpPK;
import ird.demo.domain.UsrGrpTranPK;
import ird.demo.repository.MenuRepository;
import ird.demo.repository.TranGrpRepository;
import ird.demo.repository.TranGrpTranRepository;
import ird.demo.repository.TransactionRepository;
import ird.demo.repository.UserRepository;
import ird.demo.repository.UsrGrpMenuRepository;
import ird.demo.repository.UsrGrpRepository;
import ird.demo.repository.UsrGrpTranGrpRepository;
import ird.demo.repository.UsrGrpTranRepository;
import ird.demo.service.CommonService;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;

import com.google.inject.internal.Lists;

@Service
public class CommonServiceImpl implements CommonService{
	@Autowired
	private UsrGrpMenuRepository usrGrpMenuRepository;

	@Autowired
	private TransactionRepository transactionRepository;

	@Autowired
	private MenuRepository menuRepository;

	@Autowired
	private UsrGrpTranRepository usrGrpTranRepository;

	@Autowired
	private TranGrpTranRepository tranGrpTranRepository;

	@Autowired
	private UserRepository userRepository;

	@Autowired
	private UsrGrpTranGrpRepository usrGrpTranGrpRepository;

	@Autowired
	private TranGrpRepository tranGrpRepository;

	@Autowired
	private UsrGrpRepository usrGrpRepository;
	
	@Autowired
	private ApplicationContext context;

	public UsrGrpMenu findUsrGrpMenu(UsrGrpMenuPK pk){
		return usrGrpMenuRepository.findOne(pk);
	}

	public Transaction findTransaction(String pk){
		return transactionRepository.findOne(pk);
	}

	public Menu findMenuById(String pk){
		return menuRepository.findOne(pk);
	}
	
	public List<Menu> findAllMenu(){
		return Lists.newArrayList(menuRepository.findAllMenu());
	}
	
	public Menu findMenuByTranId(String tranId){
		return menuRepository.findMenuByTranId(tranId);
	}
	
	public void updateMenu(Menu menu){
		Menu updatedMenu = menuRepository.save(menu);
		System.out.println("updatedMenu="+updatedMenu);
	}

	public UsrGrpTran findUsrGrpTran(UsrGrpTranPK pk){
		return usrGrpTranRepository.findOne(pk);
	}

	public TranGrpTran findTranGrpTran(TranGrpTranPK pk){
		return tranGrpTranRepository.findOne(pk);
	}

	public User findUser(String pk){
		return userRepository.findOne(pk);
	}

	public UsrGrpTranGrp findUsrGrpTranGrp(UsrGrpTranGrpPK pk){
		return usrGrpTranGrpRepository.findOne(pk);
	}

	public TranGrp findTranGrp(String pk){
		return tranGrpRepository.findOne(pk);
	}

	public UsrGrp findUsrGrp(String pk){
		return usrGrpRepository.findOne(pk);
	}

	public List<Menu> findMenuByGrp(String usrGrp){
		return menuRepository.findMenuByGrp(usrGrp);
	}
	
	public List<Transaction> findTranInMenuByUsrGrp(String usrGrp){
		return transactionRepository.findTranInMenuByUsrGrp(usrGrp);
	}
	
	public List<Transaction> findTranInByMenuId(String menuId){
		return transactionRepository.findTranInByMenuId(menuId);
	}
	
	public AjaxResult validateNullStr(String[][] fieldErrcodeArr, Map<String,String> paraMap){
		AjaxResult result = null;
		for(String[] fieldErrcode : fieldErrcodeArr){
			String field = fieldErrcode[0];
			String errCode = fieldErrcode[1];
			if(SysUtil.isNullstr(paraMap.get(field))){
				String errMsg = context.getMessage(errCode, null, Locale.getDefault());
				result = new AjaxResult(DemoConstant.FAIL, field, errMsg);
				return result;
			}
			
		}
		return new AjaxResult(DemoConstant.SUCCESS, null, null);
	}
}